package com.aoip.telemetry.telemetry.configureui.business.analysis.addition.controller;

import com.aoip.telemetry.telemetry.configureui.business.analysis.addition.service.AdditionFieldsService;
import com.aoip.telemetry.telemetry.configureui.common.annotation.Log;
import com.aoip.telemetry.telemetry.configureui.common.core.controller.BaseController;
import com.aoip.telemetry.telemetry.core.entity.AjaxResult;
import com.aoip.telemetry.telemetry.configureui.common.core.page.TableDataInfo;
import com.aoip.telemetry.telemetry.configureui.common.enums.BusinessType;
import com.aoip.telemetry.telemetry.entity.element.ElementAdditionFiledConfig;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 针对解析入库需要的额外字段如监测点编码，时间等 进行配置。
 *
 * @author chenbai
 * @version 1.0
 * @date 2022/1/5 9:36
 */
@RestController
public class AdditionFieldsController extends BaseController {

    @Resource
    private AdditionFieldsService service;

    /**
     * * 获取额外字段配置分页列表
     *
     * @param entity 附加字段实体
     * @return com.aoip.telemetry.telemetry.configureui.common.core.page.TableDataInfo
     * @author chenbai
     * @date 2022/1/5
     **/
    @GetMapping("/v1/GET/addition/field/page")
    public TableDataInfo page(ElementAdditionFiledConfig entity) {
        startPage();
        List<ElementAdditionFiledConfig> list = service.list(entity);
        return getDataTable(list);
    }

    /**
     * 获取单个接入测站详情
     *
     * @param id 数据id
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2022/1/5
     **/
    @GetMapping("/v1/GET/addition/field/{id}")
    public AjaxResult elementAdditionField(@PathVariable String id) {
        return AjaxResult.success(service.elementAdditionField(id));
    }

    /**
     * 查询所有接入测站
     *
     * @param entity 附加字段实体
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2021/12/31
     **/
    @GetMapping("/v1/GET/addition/field/list")
    public AjaxResult list(ElementAdditionFiledConfig entity) {
        List<ElementAdditionFiledConfig> list = service.list(entity);
        return AjaxResult.success(list);
    }

    /**
     * 新增附加字段信息
     *
     * @param entity 附加字段实体
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2022/1/5
     **/
    @Log(title = "附加字段信息", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/addition/field")
    public AjaxResult add(@Validated @RequestBody ElementAdditionFiledConfig entity) {
        return toAjax(service.insert(entity));
    }

    /**
     * 修改附加字段信息
     *
     * @param entity 附加字段实体
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     * @author chenbai
     * @date 2022/1/5
     **/
    @Log(title = "附加字段信息", businessType = BusinessType.UPDATE)
    @PutMapping("/v1/PUT/addition/field")
    public AjaxResult edit(@Validated @RequestBody ElementAdditionFiledConfig entity) {
        return toAjax(service.update(entity));
    }

    /**
     * 初始化测站缓存数据到redis中，数据格式（id, elementAdditionField）,（补齐字段数据Id，补齐字段实体）
     */
    @Log(title = "初始化配置数据到redis", businessType = BusinessType.INSERT)
    @PostMapping("/v1/POST/addition/field/cache")
    public AjaxResult initCache() {
        service.initCache();
        return AjaxResult.success();
    }

    /**
     * 删除附加字段信息
     * @author chenbai
     * @date 2022/1/5
     * @param ids 一个或多个数据id集合
     * @return com.aoip.telemetry.telemetry.core.entity.AjaxResult
     **/
    @Log(title = "附加字段信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/v1/DELETE/addition/field/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        service.delete(ids);
        return success();
    }
}
